package com.totcy.atom.model;

import android.content.Context;

import com.totcy.atom.bean.User;
import com.totcy.atom.bean.response.VoidResponse;
import com.totcy.atom.net.HttpManager;
import com.totcy.atom.net.subscriber.BaseHttpSubscriber;
import com.totcy.atom.presenter.contract.LoginContract;
import com.totcy.atom.presenter.contract.RegisterContract;
import com.totcy.baselibrary.base.mvpbase.BaseModelImpl;

import java.util.Map;
import java.util.TreeMap;

/**
 * Description 用户注册 model层
 * Author: tu
 * Date: 2018-10-26
 * Time: 16:13
 */
public class RegisterModel extends BaseModelImpl implements RegisterContract.Model {

    private BaseHttpSubscriber<User> subscriber;

    public RegisterModel(Context context) {
        super(context);
    }

    @Override
    public void canCelRequest() {
        if (subscriber != null)
            subscriber.unSubscribe();
    }

    @Override
    public void doRegister(Map<String, Object> params, final DataCallback callback) {
        //手机号 密码 非空校验
        //params

        HttpManager.getApiService(mContext)
                .doRegister(params)
                .compose(HttpManager.<User>applySchedulers())
                .compose(HttpManager.<User>handleResult())
                .subscribe(subscriber = new BaseHttpSubscriber<User>() {
                    @Override
                    public void onSuccess(User voidResponse) {
                        callback.onRegisterResult(voidResponse);
                    }

                    @Override
                    public void onError(int code, String errorMsg) {
                        callback.onRegisterFail(errorMsg);
                    }
                });
    }
}
